//
//  MainViewController.m
//  新浪微博OC
//
//  Created by  justinchou on 16/2/23.
//  Copyright © 2016年  justinchou. All rights reserved.
//

#import "MainViewController.h"
#import "HomeViewController.h"
#import "MessageViewController.h"
#import "DiscoverViewController.h"
#import "ProfileViewController.h"

@interface MainViewController ()

@property (nonatomic, strong) UIButton *composeButton;

@end

@implementation MainViewController

-(UIButton *)composeButton{
    if (_composeButton == nil) {
        _composeButton = [[UIButton alloc] init];
    }
    return _composeButton;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self addChildViewControllers];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self setupComposeButton];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark:- 监听方法
- (void)clickComposeButton {
    NSLog(@"点击撰写按钮");
}


/// 设置撰写按钮
-(void)setupComposeButton {
    
    [self.composeButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [self.composeButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
    
    [self.composeButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [self.composeButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    
    [self.composeButton addTarget:self action:@selector(clickComposeButton) forControlEvents:UIControlEventTouchUpInside];
    
    [self.composeButton sizeToFit];
    
    [self.tabBar addSubview:self.composeButton];
    
    int width = self.tabBar.bounds.size.width / self.childViewControllers.count - 1;
    
    self.composeButton.frame = CGRectInset(self.tabBar.bounds, 2 * width, 0);
    
    [self.tabBar bringSubviewToFront:self.composeButton];
}

#pragma mark - 设置子控制器方法
/// 设置子控制器们
-(void) addChildViewControllers {
    [self addChildViewController:[[HomeViewController alloc] init] Title:@"首页" Image:@"tabbar_home"];
    [self addChildViewController:[[MessageViewController alloc] init] Title:@"消息" Image:@"tabbar_message_center"];
    [self addChildViewController:[[UIViewController alloc] init]];
    [self addChildViewController:[[DiscoverViewController alloc] init] Title:@"发现" Image:@"tabbar_discover"];
    [self addChildViewController:[[ProfileViewController alloc] init] Title:@"我" Image:@"tabbar_profile"];
}

/// 设置子控制器
///
/// @param vc        控制器类型
/// @param title     标题
/// @param imageName 图片
- (void)addChildViewController:(UIViewController *)vc Title:(NSString *)title Image:(NSString *)imageName {
    
    vc.title = title;
    
    [vc.tabBarItem setTitle:title];
    [vc.tabBarItem setImage:[UIImage imageNamed:imageName]];
    //设置tabbar的图片，需要注意渲染模式，要改成original才行
    UIImage *selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%@_selected", imageName]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.selectedImage = selectedImage;
    
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor orangeColor]} forState:UIControlStateSelected];
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    
    [self addChildViewController:nav];
    
}

@end
